package Client;

import utils.AccountSendMessageInfoList;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


/**
 * 一个登录的用户创建一个用户线程，一个用户线程有一个Socket用于和服务器通信
 */
public class User implements Serializable {
    LoginUI loginUI;
    MainUI mainUI;
    ChatUI chatUI;
    GetFriendArrayListActionListener getFriendArrayListActionListener;
    private String userId = "";
    private String userPwd = "";
    /**
     * 用户的好友列表
     */
    private ArrayList<String> friendIdList;
    /**
     * 用户添加好友窗口存在的标志
     */
    private boolean addFriendUIexist = false;
    /**
     * 用户的聊天窗口列表
     */
    private List<String> chatUIList = new ArrayList<String>();
    /**获取当前用户所有消息列表的类的对象*/
    SendRequestsRegularlyToGetMessage sendRequestsRegularlyToGetMessage;
    /**聊天消息列表*/
    private AccountSendMessageInfoList messageList = new AccountSendMessageInfoList();

    private int[] messageOneListLength;

    public User(String userId, String password) {
        this.userId = userId;
        this.userPwd = password;
    }

    public User(String userId, String password, LoginUI loginUI) {
        this.userId = userId;
        this.userPwd = password;
        this.loginUI = loginUI;
        getFriendList();
        try {
            setSendRequestsRegularlyToGetMessage();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public User(String userId, String password,int i) {
        this.userId = userId;
        this.userPwd = password;
        getFriendList();
        try {
            setSendRequestsRegularlyToGetMessage();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void setSendRequestsRegularlyToGetMessage() throws IOException {
        sendRequestsRegularlyToGetMessage=new SendRequestsRegularlyToGetMessage(this);
    }

    public void initMessageOneListLength(){
        messageOneListLength = new int[friendIdList.size()];
    }

    public int getMessageOneListLength(int index){
        return messageOneListLength[index];
    }

    public void setMessageList(AccountSendMessageInfoList messageInfoList){
        initMessageOneListLength();
        for(int i=0;i<messageList.size();i++){
            messageOneListLength[i]=messageList.get(i).size();//在更新消息列表之前保存消息列表中各子列表的消息数量
        }
        messageList=messageInfoList;

    }

    public void getNewMessage(){
        for (int i = 0; i < getMessageListLength(); i++) {
            if (mainUI.user.getMessageListElementLength(i) > mainUI.user.getMessageOneListLength(i)){

            }
        }
    }

    public int getMessageListLength(){
        return messageList.size();
    }

    public int getMessageListElementLength(int i){
        return messageList.get(i).size();
    }

    public AccountSendMessageInfoList getMessageList(){
        return this.messageList;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String password) {
        this.userPwd = password;
    }

    public void getFriendList() {
        getFriendArrayListActionListener = new GetFriendArrayListActionListener(this.userId);
        friendIdList = getFriendArrayListActionListener.getFriendArrayList().getFriendArrayList();
    }

    public List<String> getFriendIdList(){
        return friendIdList;
    }

    public void setFriendIdList(ArrayList<String> friendIdList) {
        this.friendIdList = friendIdList;
    }

    public boolean haveThisFriend(String friendId) {
        return this.friendIdList.contains(friendId);
    }

    public boolean isAddFriendUIexist() {
        return addFriendUIexist;
    }

    public void setAddFriendUIexist(boolean addFriendUIexist) {
        this.addFriendUIexist = addFriendUIexist;
    }

    public List<String> getChatUIList() {
        return chatUIList;
    }

    public void addChatUIList(ChatUI chatUI) {
        this.chatUIList.add(chatUI.getChatUIId());
    }

    public boolean haveThisChatUI(String chatUIId) {
        return this.chatUIList.contains(chatUIId);
    }

    public void removeChatUIFromChatUIList(String closeChatUIId) {
        //删除当前项
        this.chatUIList.removeIf(item -> item.equals(closeChatUIId));
    }

    public MainUI getMainUI() {
        return mainUI;
    }

    public void setMainUI(MainUI mainUI) {
        this.mainUI = mainUI;
    }

    public ChatUI getChatUI() {
        return chatUI;
    }

    public void setChatUI(ChatUI chatUI) {
        this.chatUI = chatUI;
    }

}


